# 自定义栈异常
class StackError(Exception):
    pass


class Node:
    def __init__(self, val, next_node=None):
        """
        链表结点类
        :param val: 为结点的数据
        :param next_node: obj类型，指向下一个结点
        """
        self.val = val  # 有用数据
        self.next = next_node


class LStack:
    def __init__(self):
        self.top = None  # 标记栈顶位置
        self.length = 0

    def is_empty(self):
        """
        判断栈是否为空
        :return: bool类型，空返回True，否则返回False
        """
        return self.top is None

    def push(self, value):
        """
        入栈
        :param value: 传入的数据
        :return:
        """
        self.top = Node(value, self.top)

    def pop(self):
        """
        出栈
        :return: 返回出栈的结点
        """
        if self.is_empty():
            raise StackError("stack is empty")
        p = self.top
        self.top = self.top.next
        return p.val

    def top(self):
        """
        查看栈顶元素
        :return: 返回栈顶元素
        """
        if not self.is_empty():
            return self.top().val
        raise StackError("stack is empty")

    def reverse_poland(self, target):
        list_target = target.split(" ")
        operator = ("+", "-")
        for item in list_target:
            if item == "p":
                break
            if item not in operator:
                self.push(item)
            else:
                a = self.pop()
                b = self.pop()
                arithmetic = a+item+b
                result = eval(arithmetic)
                self.push(str(result))
        return self.pop()


if __name__ == "__main__":
    st = LStack()
    print(st.is_empty())

    print(st.reverse_poland("34 32 + 65 -"))
    l = "32 34   +".split(" ")
    print(l)